import axios from 'axios'
import { Toast } from 'vant'

const baseURL = '/api'

const http = axios.create({
    baseURL,
    timeout: 10000
})

http.interceptors.request.use(
    config => {
        const token = window.localStorage.getItem('token')
        if (token) {
            config.params = {
                token
            }
        }
        return config
    },
    error => Promise.reject(error)
)
http.interceptors.response.use(
    response => {
        const res = response.data
        if(response.status !== 200) {
            return Promise.reject(new Error(res.success || 'Error'))
        } else {
            if (+res.code === 200) {
                return res.result
            } else {
                (Toast as any)(res.success)
                return false
            }
        }
    }
)

export default http